package msr.frontend;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import msr.exception.ExpertException;
import msr.object.Product;
import msr.service.OracleService;
import msr.util.RequestAction;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class SearchAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<Product> rtList = new ArrayList<Product>();
	private OracleService oService = new OracleService();
	Map<String, Object> session = ActionContext.getContext().getSession();
	private static final Logger log = Logger.getLogger(SearchAction.class
			.getName());
	private String username;
	private String passwd;
	private String productname;

	public String execute() {
		System.out.println("action " + this.getClass());
		// get all product
		this.setUsername((String) session.get("username"));
		this.setPasswd((String) session.get("passwd"));
		// try {
		if (productname == null) {
			System.out.println("search all product");
			rtList = oService.searchProduct(RequestAction.PRODUCT_ALL,
					RequestAction.GET_ALL_PRODUCT);
		} else if (productname.equals("")) {
			System.out.println("search all product");
			rtList = oService.searchProduct(RequestAction.PRODUCT_ALL,
					RequestAction.GET_ALL_PRODUCT);
		} else {
			System.out.println("search product by name " + productname);
			rtList = oService.searchProduct(productname,
					RequestAction.FEED_NAME);
		}
		// } catch (ExpertException ex) {
		// log.info(ex.getCauses());
		// addActionError(getText("error.search"));
		// return ERROR;
		// }
		// ;
		if (rtList == null || rtList.size() == 0) {
			addActionError(getText("error.search"));
			return ERROR;
		} else
			return SUCCESS;
	}

	public List<Product> getRtList() {
		return rtList;
	}

	public void setRtList(List<Product> rtList) {
		this.rtList = rtList;
	}

	public String getProductname() {
		return productname;
	}

	public void setProductname(String productname) {
		this.productname = productname;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

}
